package com.linyaonan.leetcode.easy._1022;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lin
 * @date: 2020/1/12
 */
public class SumOfRoot {
    public int sumRootToLeaf(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return sum(root, 0);
    }

    private int sum(TreeNode root, int i) {
        i <<= 1;
        i += root.val;
        if (root.left == null && root.right == null) {
            return i;
        }
        int res = 0;
        if (root.left != null) {
            res += sum(root.left, i);
        }
        if (root.right != null) {
            res += sum(root.right, i);
        }
        return res;
    }

}
